package cn.kgc.ybis.controller;


import cn.kgc.ybis.common.Dto;
import cn.kgc.ybis.pojo.SchoolManager;
import cn.kgc.ybis.pojo.Teacher;
import cn.kgc.ybis.pojo.User;
import cn.kgc.ybis.service.TeacherService;
import cn.kgc.ybis.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * session中的数据teacher，schoolManager
 *
 */
@Controller
public class UserController {
    @Resource
    UserService userService;
    @Resource
    TeacherService teacherService;
    @RequestMapping("/login")
    public String login(
            Model model,
            @RequestParam(value = "uPhone", required = false) String uPhone,
            @RequestParam(value = "uPassword", required = false) String uPassword,
            @RequestParam(value = "role", required = false) Integer role,
            @RequestParam(value = "tPhone", required = false) String tPhone,
            @RequestParam(value = "tPassword", required = false) String tPassword,
            @RequestParam(value = "smPhone", required = false) String smPhone,
            @RequestParam(value = "smPassword", required = false) String smPassword,
            HttpSession session
    ) {
        Dto user = userService.login(uPhone, uPassword);
        Dto usr1 = userService.login2(tPhone, tPassword);
        Dto user2 = userService.login4(smPhone, smPassword);
        //当登录用户为老师的时候
        if (role == 1) {
            model.addAttribute("tPhone", tPhone);
            model.addAttribute("tPassword", tPassword);
            model.addAttribute("role", role);
            //往session中传老师对象;
            Teacher teacher = userService.selectTeacherByEx(tPhone, tPassword);
            session.setAttribute("teacher",teacher);
            System.out.println("ok");
            return "index";
        }
        //当登录用户为学校管理员时候
        if (role == 4) {
            model.addAttribute("smPhone", smPhone);
            model.addAttribute("smPassword", smPassword);
            model.addAttribute("role", role);
            //往session中传值穿学校管理员对象
            SchoolManager schoolManager = userService.selectSchoolManager(smPhone, smPassword);
            session.setAttribute("schoolManager",schoolManager);
            System.out.println("ok");
            return "index";
        }
        //当登录为普通用户时候
        if (Boolean.valueOf(user.getSuccess())) {
            model.addAttribute("uPhone", uPhone);
            model.addAttribute("uPassword", uPassword);
            User user1 = userService.selectUserByEx(uPhone, uPassword);
            session.setAttribute("user",user);
            System.out.println("成功");
            return "index";
        } else {
            System.out.println("失败");
            model.addAttribute("msg", "登录失败!");
            model.addAttribute("msg2", user.getMsg());
            model.addAttribute("ErrorCode", user.getErrorCode());
            return "denglu2";
        }
    }

    @RequestMapping("test")
    public String test() {
        return "denglu2";
    }

}
